﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace LinearAlgebra
{
    public partial class MatrixBasisChangeForm : Form
    {
        public MatrixBasisChangeForm()
        {
            InitializeComponent();
        }
        private void MatrixBasisChangeForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            SelectionForm form = new SelectionForm();
            form.Location = this.Location;
            form.Show();
        }
        private void Back_Click(object sender, EventArgs e)
        {
            Close();
        }
        private void Randomize_Click(object sender, EventArgs e)
        {
            RandomizeValues();
        }
        private void RandomizeValues()
        {
            B11_1.Text = UtilityMethods.GetRandomNumberString(20);
            B12_1.Text = UtilityMethods.GetRandomNumberString(20);
            B21_1.Text = UtilityMethods.GetRandomNumberString(20);
            B22_1.Text = UtilityMethods.GetRandomNumberString(20);

            B11_2.Text = UtilityMethods.GetRandomNumberString(20);
            B12_2.Text = UtilityMethods.GetRandomNumberString(20);
            B21_2.Text = UtilityMethods.GetRandomNumberString(20);
            B22_2.Text = UtilityMethods.GetRandomNumberString(20);
        }        
        private void FloatInput(object sender, KeyEventArgs e)
        {
            if (UtilityMethods.ValidateFloatKeyEvent((TextBox)sender, e))
                Close();
        }
        private void GetConversionMatrix_Click(object sender, EventArgs e)
        {
            float b11_1 = float.Parse(B11_1.Text);
            float b12_1 = float.Parse(B12_1.Text);
            float b21_1 = float.Parse(B21_1.Text);
            float b22_1 = float.Parse(B22_1.Text);

            float b11_2 = float.Parse(B11_2.Text);
            float b12_2 = float.Parse(B12_2.Text);
            float b21_2 = float.Parse(B21_2.Text);
            float b22_2 = float.Parse(B22_2.Text);

            float determinateOne = b11_1 * b22_1 - b12_1 * b21_1;
            float determinateTwo = b11_2 * b22_2 - b12_2 * b21_2;

            if(determinateOne == 0)
            {
                InvalidBasisDialog errorBox = new InvalidBasisDialog("The first basis");
                errorBox.ShowDialog();
                return;
            }
            else if(determinateTwo == 0)
            {
                InvalidBasisDialog errorBox = new InvalidBasisDialog("The second basis");
                errorBox.ShowDialog();
                return;
            }

            MatrixBasisChangeDialog form = new MatrixBasisChangeDialog(b11_1, b21_1, b12_1, b22_1, b11_2, b21_2, b12_2, b22_2);
            form.Location = this.Location;
            form.Show( );
        }
    }
}
